package com.msb.mall.product.service.impl;

import com.msb.common.utils.R;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.product.dao.CategoryDao;
import com.msb.mall.product.entity.CategoryEntity;
import com.msb.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 数据字典父子关联查询
     * @param params
     * @return
     */
    @Override
    public List<CategoryEntity> queryPageWithTree(Map<String, Object> params) {
        //做一个全部数据的查询
        List<CategoryEntity> list = baseMapper.selectList(null);
        //找出其中最大的父类 patent_id = 0
        List<CategoryEntity> entityList = list.stream().filter(menu -> menu.getParentCid() == 0)
                //向当前父对象的设置子类
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getMenu(categoryEntity, list));
                     return categoryEntity;
                })
                //根据sort排序
                .sorted((menu1,menu2) -> menu1.getSort()==null?0: menu1.getSort() - (menu2.getSort()==null?0: menu2.getSort()) )
                //结束，生成新的list集合
                .collect(Collectors.toList());
        //根据找出关联的子类
        return entityList;
    }

    /**
     * 类别批量删除
     * @param catIds
     */
    @Override
    public void removeMenusByIds(List<Long> catIds) {
        //TODO 其他删除前的业务逻辑，当前对象数据是否被引用

        baseMapper.deleteBatchIds(catIds);

    }

    /**
     * 递归父类匹配查询子类
     * @param menu
     * @param list
     * @return
     */
    private List<CategoryEntity> getMenu(CategoryEntity menu, List<CategoryEntity> list) {
        //根据当前的对象， 父类ID parent_cid 是否和子类ID cat_id 匹配
        List<CategoryEntity> entityList = list.stream().filter(category ->  category.getParentCid() == menu.getCatId())
                //当满足条件是通过递归 循环完成子类 childrens 的赋值
                .map(categoryEntity -> {
                    categoryEntity.setChildren(getMenu(categoryEntity, list));
                    return categoryEntity;
                })
                //根据sort排序
                .sorted((categoryEntity1,categoryEntity2) -> categoryEntity1.getSort()==null?0 : categoryEntity1.getSort() - (categoryEntity2.getSort()==null?0 : categoryEntity2.getSort()) )
                //结束，生成新的list集合
                .collect(Collectors.toList());
        //返回结果
        return entityList;
    }


}